import {
    async
} from "../../lib/runtime/runtime.js"
import {
    request
} from "../../request/index.js"
// pages/search/search.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        text: "",
        advice: [],
        isSearch:false
    },
    // 取消搜索
    handleCancelSearch() {
        this.setData({
            text: "",
            isSearch:false,
            advice:[]
        })
    },
    timer:null,
    // 输入建议
    handleInputAdvice(e) {
        const text = e.detail.value.toString();
        let isSearch = false;
        if (text) isSearch = true;
        this.setData({
            text,
            isSearch
        });
        // 防抖处理
        clearTimeout(this.time)
        this.time = setTimeout(() => {
            this.setInputAdvice(text)
        }, 1000)
    },
    // 获取输入框内容建议
    async setInputAdvice(text) {
        const res = await request({
            url: "/goods/qsearch",
            data: {
                query: text
            }
        })
        const advice = res.data.message
        if (advice.length===0) {
            wx.showToast({
              title: '未找到内容',
              icon:'error'
            })
        } else {
            this.setData({
                advice
            })
        }
    }
})